package com.smartwasser.yunhtgl.apphtgl.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.utils.AppConfigSP;
import com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils;
import com.smartwasser.yunhtgl.apphtgl.widget.CustomToast;
import com.smartwasser.yunhtgl.apphtgl.widget.GestureLockViewGroup;
import com.smartwasser.yunhtgl.apphtgl.widget.GestureLockViewGroup.OnGestureLockViewListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 手势解锁
 */
public class GestureGetActivity extends BaseActivity implements OnClickListener {
    private GestureLockViewGroup mGestureLockViewGroup;
    private int[] mGesturePasswords;
    private int mType = 0;
    private int mCount = 2;
    @BindView(R.id.agp_tips)
    TextView mTipsTv;
    @BindView(R.id.agp_user_login)
    View userLogin;

    @Override
    public void setContentLayout() {
        Intent intent = getIntent();
        if (intent != null) {
            mType = intent.getIntExtra("type", 0);
        }
        AppConfigSP sp = new AppConfigSP(this);
        String gesturePassword = sp.getGesturePassword();
        if (TextUtils.isEmpty(gesturePassword)) {
            startSetGesturePasswordActivity(mType);
        } else {
            mGesturePasswords = BaseUtils.convertStrToNumber(gesturePassword);
            if (mGesturePasswords != null) {
                setContentView(R.layout.activity_gesture_get);
                initView();
            } else {
                startSetGesturePasswordActivity(mType);
            }
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        if (mType == 1) {
            mTipsTv.setText("请输入旧手势密码");
            userLogin.setVisibility(View.INVISIBLE);
        }
        userLogin.setOnClickListener(this);
        mGestureLockViewGroup = findViewById(R.id.agp_gesture_lock);
        mGestureLockViewGroup.setOnGestureLockViewListener(mOnGestureLockViewListener);
        mGestureLockViewGroup.setAnswer(mGesturePasswords);
    }

    private void startLoginActivity() {
        Intent intent = new Intent(GestureGetActivity.this, LoginActivity.class);
        intent.putExtra("isLoginFail", true);
        startActivity(intent);
        finish();
    }

    private void startSetGesturePasswordActivity(int type) {
        Intent intent = new Intent(this, GestureSetActivity.class);
        intent.putExtra("type", type);
        startActivity(intent);
        finish();
    }

    private void startHomeActivity() {
        startActivity(SysActivity.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.agp_user_login:
                startLoginActivity();
                break;

            default:
                break;
        }
    }

    private OnGestureLockViewListener mOnGestureLockViewListener = new OnGestureLockViewListener() {
        @Override
        public void onUnmatchedExceedBoundary() {

        }

        @Override
        public void onGestureEvent(boolean matched) {
            mGestureLockViewGroup.validationResults(matched);
            if (matched) {
                if (mType == 0) {
                    startHomeActivity();
                } else {
                    startSetGesturePasswordActivity(mType);
                }
            } else {
                mTipsTv.setText("密码错误，重新输入");
                if (mCount <= 0) {
                    startLoginActivity();
                    CustomToast.showToast(getApplication(), "手势错误，账号登录");
                }
            }
            mCount = mCount - 1;
        }

        @Override
        public void onBlockSelected(int cId) {
            //mGestureDisplayView.addChoose(cId);
        }

        @Override
        public void onDrawFinish(List<Integer> choose) {
            //mGestureDisplayView.setInputOver(true);
        }
    };
}
